import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-local';
import { LOCAL_AUTH_TYPE } from '@app/common';
// user
import { UserService } from '../../system/user/user.service';

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy, LOCAL_AUTH_TYPE) {
  constructor(private readonly userService: UserService) {
    super({
      usernameField: 'username',
      passwordField: 'password',
    });
  }

  async validate(account: string, pwd: string): Promise<any> {
    // 策略内校验用户信息
    const user = await this.userService.validateUser(account, pwd);
    if (!user) {
      throw new UnauthorizedException();
    }
    return user;
  }
}
